<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialog"
    :close-on-click-modal="false"
    title="字段变更"
    append-to-body
    width="95%"
    @close="cancel"
  >
    <div class="app-container">
      <el-button v-if="createFlag === false" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="doBatchUpdate">保存</el-button>
      <el-button v-if="createFlag === false" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="doBatchValidate">批量校验</el-button>
      <el-button v-if="createFlag === false" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="doSubmitBPMS">提交审核</el-button>
      <el-button v-if="createFlag === true" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="downloadExcelTemplate(parent.$route.name, 1)">下载导入模板</el-button>
      <el-button v-if="createFlag === true" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="exportTempDataExcel(data, parent.$route.name)">导出</el-button>
      <el-button v-if="createFlag === true" class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="doBatchCreate">保存</el-button>
      <el-button class="filter-item" size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" round @click="doReadInfo">获取Item信息</el-button>
      <div v-if="createFlag === true" style="margin-bottom:5px;">
        <el-form :inline="true" size="small" label-width="auto">
          <el-form-item label="商品号：">
            <el-input v-model="itemNbrTxt" placeholder="输入商品号以行分隔" rows="5" type="textarea" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="采购ID：">
            <el-select v-model="buyerId" style="width: 280px;">
              <el-option v-for="bid in buyerList" :key="bid" :label="bid" :value="bid" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" size="small" border :height="tableHeight" style="width: 100%" @header-dragend="colChange">
        <el-table-column type="index" align="center" fixed width="55" label="序号" />
        <el-table-column v-if="createFlag === true" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.prevent="deleteRow(scope.$index)">移除该项</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="createFlag === false" label="校验信息">
          <template slot-scope="scope">
            <!--<el-popover
              v-if="scope.row.errorMsg && JSON.parse(scope.row.errorMsg).length > 0"
              placement="bottom"
              title="校验信息"
              trigger="click"
            >
              <div style="height:400px;overflow:auto;">
                <div v-for="(msg,index) in JSON.parse(scope.row.errorMsg)" :key="index" :index="index">
                  <span>{{ (index + 1) + '.' + msg }}</span>
                </div>
              </div>
              <el-button slot="reference" size="mini" type="text" style="color:#fc1111">点击查看</el-button>
            </el-popover>-->
            <validateMsg :item="scope.row" :type="3" />
          </template>
        </el-table-column>
        <el-table-column label="商品号" fixed prop="itemNbr" :width="colWidths.itemNbr" />
        <el-table-column label="部门" prop="deptNbr" :width="colWidths.deptNbr" />
        <el-table-column label="状态" prop="itemStatusCode" :width="colWidths.itemStatusCode" />
        <el-table-column label="商品类型" prop="itemTypeCode" :width="colWidths.itemTypeCode" />
        <el-table-column label="供应商箱包数量" prop="vnpkQty" :width="colWidths.vnpkQty" />
        <el-table-column label="供应商编号" prop="vendorNbr" :width="colWidths.vendorNbr" />
        <el-table-column label="供应商名称" prop="vendorName" :width="colWidths.vendorName" />
        <el-table-column label="细分类" prop="finelineNbr" :width="colWidths.finelineNbr" />
        <el-table-column label="CHG_细分类" prop="chg_finelineNbr" :width="colWidths.chg_finelineNbr">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_finelineNbr" class="filter-item" :width="colWidths.chg_finelineNbr" />
          </template>
        </el-table-column>
        <el-table-column label="商品说明1" prop="item1Desc" :width="colWidths.item1Desc" />
        <el-table-column label="CHG_商品说明1" prop="chg_item1Desc" :width="colWidths.chg_item1Desc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_item1Desc" class="filter-item" :width="colWidths.chg_item1Desc" />
          </template>
        </el-table-column>
        <el-table-column label="条码说明" prop="upcDesc" :width="colWidths.upcDesc" />
        <el-table-column label="CHG_条码说明" prop="chg_upcDesc" :width="colWidths.chg_upcDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_upcDesc" class="filter-item" :width="colWidths.chg_upcDesc" />
          </template>
        </el-table-column>
        <el-table-column label="旗标说明" prop="signingDesc" :width="colWidths.signingDesc" />
        <el-table-column label="CHG_旗标说明" prop="chg_signingDesc" :width="colWidths.chg_signingDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_signingDesc" class="filter-item" :width="colWidths.chg_signingDesc" />
          </template>
        </el-table-column>
        <el-table-column label="较长商品描述" prop="item1DescLength" :width="colWidths.item1DescLength" />
        <el-table-column label="CHG_较长商品描述" prop="chg_item1DescLength" :width="colWidths.chg_item1DescLength">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_item1DescLength" class="filter-item" :width="colWidths.chg_item1DescLength" />
          </template>
        </el-table-column>
        <el-table-column label="商品说明2" prop="item2Desc" :width="colWidths.item2Desc" />
        <el-table-column label="CHG_商品说明2" prop="chg_item2Desc" :width="colWidths.chg_item2Desc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_item2Desc" class="filter-item" :width="colWidths.chg_item2Desc" />
          </template>
        </el-table-column>
        <el-table-column label="货架2/规格" prop="shlflbl2SizeDesc" :width="colWidths.shlflbl2SizeDesc" />
        <el-table-column label="CHG_货架2/规格" prop="chg_shlflbl2SizeDesc" :width="colWidths.chg_shlflbl2SizeDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_shlflbl2SizeDesc" class="filter-item" :width="colWidths.chg_shlflbl2SizeDesc" />
          </template>
        </el-table-column>
        <el-table-column label="规格型号" prop="shlflbl3SizeDesc" :width="colWidths.shlflbl3SizeDesc" />
        <el-table-column label="CHG_规格型号" prop="chg_shlflbl3SizeDesc" :width="colWidths.chg_shlflbl3SizeDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_shlflbl3SizeDesc" class="filter-item" :width="colWidths.chg_shlflbl3SizeDesc" />
          </template>
        </el-table-column>
        <el-table-column label="条码" prop="upcNbr" :width="colWidths.upcNbr" />
        <el-table-column label="CHG_条码" prop="chg_upcNbr" :width="colWidths.chg_upcNbr">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_upcNbr" class="filter-item" :width="colWidths.chg_upcNbr" />
          </template>
        </el-table-column>
        <el-table-column label="供应商之商品代码" prop="vendorStockId" :width="colWidths.vendorStockId" />
        <el-table-column label="CHG_供应商之商品代码" prop="chg_vendorStockId" :width="colWidths.chg_vendorStockId">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vendorStockId" class="filter-item" :width="colWidths.chg_vendorStockId" />
          </template>
        </el-table-column>
        <el-table-column label="商品生效日期" prop="itemOrdEffDate" :width="colWidths.itemOrdEffDate" />
        <el-table-column label="CHG_商品生效日期" prop="chg_itemOrdEffDate" :width="colWidths.chg_itemOrdEffDate">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemOrdEffDate" class="filter-item" :width="colWidths.chg_itemOrdEffDate" />
          </template>
        </el-table-column>
        <el-table-column label="商品失效日期" prop="itemExpireDate" :width="colWidths.itemExpireDate" />
        <el-table-column label="CHG_商品失效日期" prop="chg_itemExpireDate" :width="colWidths.chg_itemExpireDate">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemExpireDate" class="filter-item" :width="colWidths.chg_itemExpireDate" />
          </template>
        </el-table-column>
        <el-table-column label="进项税" prop="vatIn" :width="colWidths.vatIn" />
        <el-table-column label="CHG_进项税" prop="chg_vatIn" :width="colWidths.chg_vatIn">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vatIn" class="filter-item" :width="colWidths.chg_vatIn" />
          </template>
        </el-table-column>
        <el-table-column label="销项税" prop="vatOut" :width="colWidths.vatOut" />
        <el-table-column label="CHG_销项税" prop="chg_vatOut" :width="colWidths.chg_vatOut">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vatOut" class="filter-item" :width="colWidths.chg_vatOut" />
          </template>
        </el-table-column>
        <el-table-column label="转运仓库代码" prop="whseAlignTypeCd" :width="colWidths.whseAlignTypeCd" />
        <el-table-column label="CHG_转运仓库代码" prop="chg_whseAlignTypeCd" :width="colWidths.chg_whseAlignTypeCd">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whseAlignTypeCd" class="filter-item" :width="colWidths.chg_whseAlignTypeCd" />
          </template>
        </el-table-column>
        <el-table-column label="货架1/颜色" prop="shlflbl1ColrDesc" :width="colWidths.shlflbl1ColrDesc" />
        <el-table-column label="CHG_货架1/颜色" prop="chg_shlflbl1ColrDesc" :width="colWidths.chg_shlflbl1ColrDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_shlflbl1ColrDesc" class="filter-item" :width="colWidths.chg_shlflbl1ColrDesc" />
          </template>
        </el-table-column>
        <el-table-column label="单位" prop="unit" :width="colWidths.unit" />
        <el-table-column label="CHG_单位" prop="chg_unit" :width="colWidths.chg_unit">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_unit" class="filter-item" :width="colWidths.chg_unit" />
          </template>
        </el-table-column>
        <el-table-column label="产地" prop="mdsDesc" :width="colWidths.mdsDesc" />
        <el-table-column label="CHG_产地" prop="chg_mdsDesc" :width="colWidths.chg_mdsDesc">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_mdsDesc" class="filter-item" :width="colWidths.chg_mdsDesc" />
          </template>
        </el-table-column>
        <el-table-column label="送往商店日期" prop="sendStoreDate" :width="colWidths.sendStoreDate" />
        <el-table-column label="CHG_送往商店日期" prop="chg_sendStoreDate" :width="colWidths.chg_sendStoreDate">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_sendStoreDate" class="filter-item" :width="colWidths.chg_sendStoreDate" />
          </template>
        </el-table-column>
        <el-table-column label="季节码" prop="seasonCode" :width="colWidths.seasonCode" />
        <el-table-column label="CHG_季节码" prop="chg_seasonCode" :width="colWidths.chg_seasonCode">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_seasonCode" class="filter-item" :width="colWidths.chg_seasonCode" />
          </template>
        </el-table-column>
        <el-table-column label="季节年份" prop="seasonYear" :width="colWidths.seasonYear" />
        <el-table-column label="CHG_季节年份" prop="chg_seasonYear" :width="colWidths.chg_seasonYear">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_seasonYear" class="filter-item" :width="colWidths.chg_seasonYear" />
          </template>
        </el-table-column>
        <el-table-column label="配色配码" prop="colorMatch" :width="colWidths.colorMatch" />
        <el-table-column label="CHG_配色配码" prop="chg_colorMatch" :width="colWidths.chg_colorMatch">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_colorMatch" class="filter-item" :width="colWidths.chg_colorMatch" />
          </template>
        </el-table-column>
        <el-table-column label="采购地区" prop="buyingRegionCode" :width="colWidths.buyingRegionCode" />
        <el-table-column label="CHG_采购地区" prop="chg_buyingRegionCode" :width="colWidths.chg_buyingRegionCode">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_buyingRegionCode" class="filter-item" :width="colWidths.chg_buyingRegionCode" />
          </template>
        </el-table-column>
        <el-table-column label="商品高度" prop="itemHeightQty" :width="colWidths.itemHeightQty" />
        <el-table-column label="CHG_商品高度" prop="chg_itemHeightQty" :width="colWidths.chg_itemHeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemHeightQty" class="filter-item" :width="colWidths.chg_itemHeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="商品宽度" prop="itemWidthQty" :width="colWidths.itemWidthQty" />
        <el-table-column label="CHG_商品宽度" prop="chg_itemWidthQty" :width="colWidths.chg_itemWidthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemWidthQty" class="filter-item" :width="colWidths.chg_itemWidthQty" />
          </template>
        </el-table-column>
        <el-table-column label="商品深度" prop="itemLengthQty" :width="colWidths.itemLengthQty" />
        <el-table-column label="CHG_商品深度" prop="chg_itemLengthQty" :width="colWidths.chg_itemLengthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemLengthQty" class="filter-item" :width="colWidths.chg_itemLengthQty" />
          </template>
        </el-table-column>
        <el-table-column label="商品重量" prop="itemWeightQty" :width="colWidths.itemWeightQty" />
        <el-table-column label="CHG_商品重量" prop="chg_itemWeightQty" :width="colWidths.chg_itemWeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemWeightQty" class="filter-item" :width="colWidths.chg_itemWeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="产品号" prop="productNbr" :width="colWidths.productNbr" />
        <el-table-column label="CHG_产品号" prop="chg_productNbr" :width="colWidths.chg_productNbr">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_productNbr" class="filter-item" :width="colWidths.chg_productNbr" />
          </template>
        </el-table-column>
        <el-table-column label="公司订单本" prop="promoOrdBookCd" :width="colWidths.promoOrdBookCd" />
        <el-table-column label="CHG_公司订单本" prop="chg_promoOrdBookCd" :width="colWidths.chg_promoOrdBookCd">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_promoOrdBookCd" class="filter-item" :width="colWidths.chg_promoOrdBookCd" />
          </template>
        </el-table-column>
        <el-table-column label="品牌" prop="brandId" :width="colWidths.brandId" />
        <el-table-column label="CHG_品牌" prop="chg_brandId" :width="colWidths.chg_brandId">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_brandId" class="filter-item" :width="colWidths.chg_brandId" />
          </template>
        </el-table-column>
        <el-table-column label="原包高度" prop="vnpkHeightQty" :width="colWidths.vnpkHeightQty" />
        <el-table-column label="CHG_原包高度" prop="chg_vnpkHeightQty" :width="colWidths.chg_vnpkHeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vnpkHeightQty" class="filter-item" :width="colWidths.chg_vnpkHeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="原包宽度" prop="vnpkWidthQty" :width="colWidths.vnpkWidthQty" />
        <el-table-column label="CHG_原包宽度" prop="chg_vnpkWidthQty" :width="colWidths.chg_vnpkWidthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vnpkWidthQty" class="filter-item" :width="colWidths.chg_vnpkWidthQty" />
          </template>
        </el-table-column>
        <el-table-column label="原包深度" prop="vnpkLengthQty" :width="colWidths.vnpkLengthQty" />
        <el-table-column label="CHG_原包深度" prop="chg_vnpkLengthQty" :width="colWidths.chg_vnpkLengthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vnpkLengthQty" class="filter-item" :width="colWidths.chg_vnpkLengthQty" />
          </template>
        </el-table-column>
        <el-table-column label="原包重量" prop="vnpkWeightQty" :width="colWidths.vnpkWeightQty" />
        <el-table-column label="CHG_原包重量" prop="chg_vnpkWeightQty" :width="colWidths.chg_vnpkWeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_vnpkWeightQty" class="filter-item" :width="colWidths.chg_vnpkWeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="仓包高度" prop="whpkHeightQty" :width="colWidths.whpkHeightQty" />
        <el-table-column label="CHG_仓包高度" prop="chg_whpkHeightQty" :width="colWidths.chg_whpkHeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whpkHeightQty" class="filter-item" :width="colWidths.chg_whpkHeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="仓包宽度" prop="whpkWidthQty" :width="colWidths.whpkWidthQty" />
        <el-table-column label="CHG_仓包宽度" prop="chg_whpkWidthQty" :width="colWidths.chg_whpkWidthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whpkWidthQty" class="filter-item" :width="colWidths.chg_whpkWidthQty" />
          </template>
        </el-table-column>
        <el-table-column label="仓包深度" prop="whpkLengthQty" :width="colWidths.whpkLengthQty" />
        <el-table-column label="CHG_仓包深度" prop="chg_whpkLengthQty" :width="colWidths.chg_whpkLengthQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whpkLengthQty" class="filter-item" :width="colWidths.chg_whpkLengthQty" />
          </template>
        </el-table-column>
        <el-table-column label="仓包重量" prop="whpkWeightQty" :width="colWidths.whpkWeightQty" />
        <el-table-column label="CHG_仓包重量" prop="chg_whpkWeightQty" :width="colWidths.chg_whpkWeightQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whpkWeightQty" class="filter-item" :width="colWidths.chg_whpkWeightQty" />
          </template>
        </el-table-column>
        <el-table-column label="仓库区" prop="whseAreaCode" :width="colWidths.whseAreaCode" />
        <el-table-column label="CHG_仓库区" prop="chg_whseAreaCode" :width="colWidths.chg_whseAreaCode">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_whseAreaCode" class="filter-item" :width="colWidths.chg_whseAreaCode" />
          </template>
        </el-table-column>
        <el-table-column label="存放区号" prop="marshalId" :width="colWidths.marshalId" />
        <el-table-column label="CHG_存放区号" prop="chg_marshalId" :width="colWidths.chg_marshalId">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_marshalId" class="filter-item" :width="colWidths.chg_marshalId" />
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="chg_remark" :width="colWidths.chg_remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_remark" class="filter-item" :width="colWidths.chg_remark" />
          </template>
        </el-table-column>
        <el-table-column label="CHG_可上传送带" prop="conveyorBeltFlag" :width="colWidths.conveyorBeltFlag" />
        <el-table-column label="仓包内装量" prop="whpkQty" :width="colWidths.whpkQty" />
        <el-table-column label="货架寿命天数" prop="shlfLifeDaysQty" :width="colWidths.shlfLifeDaysQty" />
        <el-table-column label="CHG_货架寿命天数" prop="chg_shlfLifeDaysQty" :width="colWidths.chg_shlfLifeDaysQty">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_shlfLifeDaysQty" class="filter-item" :width="colWidths.chg_shlfLifeDaysQty" />
          </template>
        </el-table-column>
        <el-table-column label="供应商箱包条码" prop="caseUpcNbr" :width="colWidths.caseUpcNbr" />
        <el-table-column label="CHG_供应商箱包条码" prop="chg_caseUpcNbr" :width="colWidths.chg_caseUpcNbr">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_caseUpcNbr" class="filter-item" :width="colWidths.chg_caseUpcNbr" />
          </template>
        </el-table-column>
        <el-table-column label="物料号" prop="itemDimUomCode" :width="colWidths.itemDimUomCode" />
        <el-table-column label="CHG_物料号" prop="chg_itemDimUomCode" :width="colWidths.chg_itemDimUomCode">
          <template slot-scope="scope">
            <el-input v-model="scope.row.chg_itemDimUomCode" class="filter-item" :width="colWidths.chg_itemDimUomCode" />
          </template>
        </el-table-column>
        <el-table-column label="成对商品标识" prop="pairingSubmitFlag" :width="colWidths.pairingSubmitFlag">
          <template slot-scope="scope">
            <el-input v-model="scope.row.pairingSubmitFlag" class="filter-item" :width="colWidths.pairingSubmitFlag" />
          </template>
        </el-table-column>
        <el-table-column label="附件" prop="filePath" :width="colWidths.filePath">
          <template slot-scope="scope">
            <i v-if="scope.row.filePath" class="el-icon-picture" style="font-size:23px;margin-right: 3px;" @click="previewFile(scope.row.filePath)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <urgentForm ref="urgentForm" :parent="parent" />
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { batchUpdateItem, batchCreateFieldItem, queryItemByItemNbr, batchValidate, queryList } from '@/api/change-item'
import { parseTime } from '@/utils/index'
import urgentForm from './urgent'
import { previewFile, exportTempDataExcel, downloadExcelTemplate } from '@/utils/item'
import initTable from '@/utils/initTable'
import validateMsg from '@/views/part/validateMsg'

export default {
  components: { urgentForm, validateMsg },
  mixins: [initTable],
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      data: [],
      loading: false,
      tableHeight: window.innerHeight * 0.7,
      createFlag: false,
      iparent: this,
      itemNbrTxt: '',
      buyerList: [],
      buyerId: ''
    }
  },
  methods: {
    previewFile,
    exportTempDataExcel,
    downloadExcelTemplate,
    cancel() {
      this.data = []
      this.itemNbrTxt = ''
      this.buyerList = []
      this.buyerId = ''
    },
    // 修改保存触发
    doBatchUpdate() {
      this.$confirm('是否变更该 ' + this.data.length + ' 条item信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        const val = []
        for (let i = 0; i < this.data.length; i++) {
          var item = this.data[i]
          item.changeTime = parseTime(new Date())
          val.push({ id: item.id, itemInfo: JSON.stringify(item) })
        }
        batchUpdateItem(val).then(() => {
          Message({
            message: '保存成功。',
            type: 'success',
            duration: 3 * 1000
          })
          this.parent.toQuery()
          this.loading = false
        }, (error) => {
          console.log(error)
          this.loading = false
        })
      })
    },
    // 删除一行
    deleteRow(index) {
      this.data.splice(index, 1)
    },
    // 新增保存触发
    doBatchCreate() {
      if (this.data.length < 1) {
        Message({
          message: '请编辑需要新增的Item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('是否新增该 ' + this.data.length + ' 条item信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          const val = []
          this.data.forEach(item => {
            item.changeTime = parseTime(new Date())
            val.push({ id: '', buyerId: this.buyerId, itemInfo: JSON.stringify(item) })
          })
          batchCreateFieldItem(val).then(() => {
            Message({
              message: '新增成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.parent.toQuery()
            this.loading = false
            this.dialog = false
          }, (error) => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    // 读取数据
    doReadInfo() {
      if (this.createFlag === true && (!this.itemNbrTxt || this.itemNbrTxt === '')) {
        Message({
          message: '请编辑需要新增的Item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else if (this.itemNbrTxt.length > 1000) {
        Message({
          message: '所需获取的信息过多，请使用Excel导入方式进行操作。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.loading = true
        let _itemNbrTxt = this.itemNbrTxt.trim() // 去掉两头空格
        _itemNbrTxt = _itemNbrTxt.replace(/[\n\r\s]/g, '-')
        const itemArray = _itemNbrTxt.split('-')
        const sourceData = []
        for (let i = 0; i < this.data.length; i++) {
          sourceData.push(this.data[i])
        }
        itemArray.forEach(_itemNbr => {
          if (_itemNbr && _itemNbr !== '') {
            this.data.push({ itemNbr: _itemNbr })
          }
        })
        const indexRemoveDate = []
        const itemNbr = []
        this.data.forEach(item => {
          itemNbr.push(item.itemNbr)
        })
        queryItemByItemNbr(itemNbr).then(res => {
          if (res) {
            res.forEach(dbItem => {
              for (let i = 0; i < this.data.length; i++) {
                if (dbItem.itemNbr.toString() === this.data[i].itemNbr.toString()) {
                  const _item = this.data[i]
                  for (const key of Object.keys(_item)) {
                    if (_item[key] && key.startsWith('chg_')) {
                      dbItem[key] = _item[key]
                    }
                  }
                  dbItem.pairingSubmitFlag = _item.pairingSubmitFlag
                  dbItem.filePath = _item.filePath
                  dbItem.errorMsg = _item.errorMsg
                  dbItem.promptMsg = _item.promptMsg
                  dbItem.id = _item.id
                  this.data.splice(i, 1, dbItem)
                  indexRemoveDate.push(i)
                }
              }
            })
            let deletFalge = 0
            var errorItem = ''
            for (let i = 0; i < this.data.length; i++) {
              if (indexRemoveDate.indexOf(i + deletFalge) === -1) {
                errorItem = errorItem + this.data[i].itemNbr.toString() + ','
                this.data.splice(i, 1)
                i -= 1
                deletFalge += 1
              }
            }
            if (errorItem && errorItem.length > 0) {
              Message({
                message: '无效商品号：' + errorItem.substring(0, errorItem.length - 1),
                type: 'warning',
                duration: 3 * 1000
              })
            }
          }
          this.loading = false
        }, (error) => {
          console.log(error)
          this.data = sourceData
          this.loading = false
        })
      }
    },
    // 批量提交审核前置
    doSubmitBPMS() {
      if (this.data.length < 1) {
        Message({
          message: '请选择需要提交审核的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        const _this = this.$refs.urgentForm
        _this.itemSize = this.data.length
        _this.changeType = this.parent.$route.name
        const ids = []
        this.data.forEach(item => {
          ids.push(item.id)
        })
        _this.bpmsParam.auditBuyerId = this.parent.auditBuyerId
        _this.ids = ids
        _this.dialog = true
      }
    },
    // 批量校验
    doBatchValidate() {
      this.$confirm('确认校验该 ' + this.data.length + ' 条item信息?', '校验数据', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        const ids = []
        this.data.forEach(item => {
          ids.push(item.id)
        })
        const val = { ids: ids, changeType: this.parent.$route.name }
        batchValidate(val).then(res => {
          queryList(val).then(dataList => {
            if (dataList) {
              const _data = []
              dataList.forEach(dataObj => {
                _data.push(JSON.parse(dataObj.itemInfo))
              })
              this.data = _data
            }
          })
          this.parent.toQuery()
          Message({
            message: '校验处理中，请稍后刷新页面进行查询。',
            type: 'success',
            duration: 3 * 1000
          })
          this.loading = false
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    }
  }
}
</script>

